// pages/balance/balance.js
var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    name: '',
    img1: '',
    img2: '',
    img3: '',
    img:false,
    docId:0,
    price:0.00,
  },
  // 上传图片1
  chooseImage: function () {
    var self = this
    wx.chooseImage({
      count: 1,
      sizeType: ['compressed'],
      sourceType: ['album'],
      success: function (res) {
        var imageSrc = res.tempFilePaths[0]
        //图片上传服务器
        wx.uploadFile({
          url: app.api.hostUrl + '/Api/User/uploadimg',
          filePath: imageSrc,
          name: 'img',
          formData: {
            uid: app.api.userId,
            imgs: self.data.img1,
          },
          header: {
            'Content-Type': 'multipart/form-data'
          },
          success: function (res) {
            var statusCode = res.statusCode;
            if (statusCode == 200) {
              wx.showToast({
                title: '上传成功',
                duration: 2000
              })
              self.setData({
                imageSrc,
                img1: res.data,
              })
            } else {
              wx.showToast({
                title: 'upload_failed !',
                duration: 2000
              })
            }
          },
          fail: function ({errMsg}) {
            console.log('uploadImage fail, errMsg is', errMsg)
            wx.showToast({
              title: '上传失败',
              duration: 2000
            })
          }
        })
      },
      fail: function ({errMsg}) {
        console.log('chooseImage fail, err is', errMsg)
        wx.showToast({
          title: '图片选取失败',
          duration: 2000
        })
      }
    })
  },
  // 上传图片2
  choose: function () {
    var self = this
    wx.chooseImage({
      count: 1,
      sizeType: ['compressed'],
      sourceType: ['album'],
      success: function (res) {
        var image = res.tempFilePaths[0];
        //图片上传服务器
        wx.uploadFile({
          url: app.api.hostUrl + '/Api/User/uploadimg',
          filePath: image,
          name: 'img',
          formData: {
            imgs: self.data.img2,
          },
          header: {
            'Content-Type': 'multipart/form-data'
          },
          success: function (res) {
            var statusCode = res.statusCode;
            if (statusCode == 200) {
              wx.showToast({
                title: '上传成功',
                duration: 2000
              })
              self.setData({
                image,
                img2: res.data,
              })
            } else {
              wx.showToast({
                title: 'upload_failed !',
                duration: 2000
              })
            }
          },
          fail: function ({errMsg}) {
            console.log('uploadImage fail, errMsg is', errMsg);
            wx.showToast({
              title: '上传失败',
              duration: 2000
            })
          }
        })
      },
      fail: function ({errMsg}) {
        console.log('chooseImage fail, err is', errMsg);
        wx.showToast({
          title: '图片选取失败',
          duration: 2000
        })
      }
    })
  },

  // 上传图片3
  choo: function () {
    var self = this
    wx.chooseImage({
      count: 1,
      sizeType: ['compressed'],
      sourceType: ['album'],
      success: function (res) {
        var imgs = res.tempFilePaths[0];
        //图片上传服务器
        wx.uploadFile({
          url: app.api.hostUrl + '/Api/User/uploadimg',
          filePath: imgs,
          name: 'img',
          formData: {
            imgs: self.data.img3,
          },
          header: {
            'Content-Type': 'multipart/form-data'
          },
          success: function (res) {
            var statusCode = res.statusCode;
            if (statusCode == 200) {
              wx.showToast({
                title: '上传成功',
                duration: 2000
              })
              self.setData({
                imgs,
                img3: res.data,
              })
            } else {
              wx.showToast({
                title: 'upload_failed !',
                duration: 2000
              })
            }
          },
          fail: function ({errMsg}) {
            console.log('uploadImage fail, errMsg is', errMsg);
            wx.showToast({
              title: '上传失败',
              duration: 2000
            })
          }
        })
      },
      fail: function ({errMsg}) {
        console.log('chooseImage fail, err is', errMsg);
        wx.showToast({
          title: '图片选取失败',
          duration: 2000
        })
      }
    })
  },

  reg:function(e){
    var that = this;
    var docId = that.data.docId;
    var name = e.detail.value.uname;
    if (!name) {
      wx.showToast({
        title: '请输入患者姓名！',
        duration: 2000
      });
      return false;
    }

    wx.request({
      url: app.api.hostUrl + '/Api/User/supply',
      method: 'post',
      data: {
        docid: docId,
        uid: app.api.userId,
        uname: name,
        content: e.detail.value.content,
        phone: e.detail.value.phone,
        casename: e.detail.value.casename,
        casehospital: e.detail.value.casehospital,
        img1: that.data.img1,
        img2: that.data.img2,
        img3: that.data.img3,
      },
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        var status = res.data.status;
        var price = that.data.price;
        console.log(res);
        if (status == 1) {
          if (parseInt(docId)>0 && parseFloat(price)>0) {
            var orderid = res.data.order_id;
            wx.navigateTo({
              url: '../payorder/payorder?orderId=' + orderid
            });
          } else {
            wx.showToast({
              title: '提交成功！',
              duration: 2000
            });
            setTimeout(function (e) {
              wx.navigateBack();
            }, 2500);
          }
        } else {
          wx.showToast({
            title: res.data.err,
            duration: 2000
          });
        }
      },
      fail: function (e) {
        wx.showToast({
          title: '网络异常！',
          duration: 2000
        });
      },
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var docId = options.docId;
    var price = options.price;
    that.setData({
      docId: docId,
      price: price,
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})